<?php
namespace app\store\controller\teacher;

use app\store\controller\Base;
use app\store\controller\Weixin;
use think\Db;
use think\Session;

class Index extends Base
{
	/**
	 * 教师首页
	 * URL ：store/teacher.index/index
     * @throws
	 * @return [type] [description]
	 */
    public function index()
    {
        //广告图
        $banner_info =  Db('banner')
            ->where('shop_id',$this->shop_id)
            ->where('is_show',1)
            ->where('is_del',0)
            ->field('pic_url,url')
            ->select();
        $this->assign('banner_info',$banner_info);

        //公告
        $notice_info = Db('notice')
            ->where('shop_id',$this->shop_id)
            ->where('is_show',1)
            ->field('title')
            ->select();
        $this->assign('notice_info',$notice_info);


        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch();
    }

    /**
     * 个人中心
     */
    public function mycenter(){
        $res = Db::name('teacher')->where('teacher_id',$this->uid)->find();
        $this->assign('res',$res);
        $this->assign('shop_id',$this->shop_id);
        return $this->fetch('myCenter');
    }

    /**
     * yang yang
     * 编辑个人信息
     * @return mixed
     */
    public function edit(){

        if(request()->isAjax()){
            $d = input();
            $data['headimgurl'] = $d['headimgurl'];
            $data['teacher_tel'] = $d['tel'];
            $data['teacher_name'] = $d['name'];
            $data['sex'] = $d['sex'];



            if($data['headimgurl'] == null || $data['headimgurl'] == "undefined"){

                $data['headimgurl'] = Db::name('teacher')->where('teacher_id',$this->uid)->value('headimgurl');
            }

            $res = Db::name('teacher')->where('teacher_id',$this->uid)->update($data);

            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }


        }

        $res= Db::name('teacher')->where('teacher_id',$this->uid)->find();
        //微信 js config
        $weixin = new Weixin($this->shop_id);
        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);
        $this->assign('jsconfig', $js_config);
        $this->assign('res', $res);
        $this->assign('shop_id',$this->shop_id);
        return  $this->fetch();
    }

    /*
     * 后台添加
     * 获取微信授权信息 openid
     * wangtiantian
     */
    public function wxinfo(){

        if(input()){
            $wx_info = json_decode(input('back_info'),true);

            $data['openid'] =  $wx_info['openid'];
            $data['nickname'] =  $wx_info['nickname'];
            $data['headimgurl'] =  $wx_info['headimgurl'];
            $data['shop_id'] =  input('shop_id');
            $data['store_id'] =  input('store_id');
            $data['teacher_id'] =  input('teacher_id');
            $data['role'] =  1;
//            $data['type'] =  input('type');

            $openid= Db::name('teacher')->where('openid',$data['openid'])->find();
            if(!$openid){
                $res = Db::name('teacher')
                    ->where('teacher_id', $data['teacher_id'])
                    ->where('shop_id',  $data['shop_id'])
                    ->where('store_id',$data['store_id'])
                    ->update($data);

                if($res){
                    Session::set('shop_id',$data['shop_id'] );
                    Session::set('uid',$data['teacher_id'] );
                    $this->success('绑定成功','index');
                }else{
                    $this->error('门店信息有误，绑定失败');
                }
            }else{
                $this->error('已有人绑定，请勿重复绑定');
            }




        }



    }
    /*
   * 微信授权
   * 获取微信授权信息 openid
   * wangtiantian
   */
    public function hand_wxinfo(){

        if(input()){
            $wx_info = json_decode(input('back_info'),true);

            $data['openid'] =  $wx_info['openid'];
            $data['nickname'] =  $wx_info['nickname'];
            $data['headimgurl'] =  $wx_info['headimgurl'];
            $data['shop_id'] =  input('shop_id');
            $data['store_id'] =  input('store_id');
//            $data['type'] =  input('type');
            $data['role'] =  1;
            $data['join_type'] =  0;

            $test = Db::name('teacher')->where('openid',$data['openid'])->find();

            if(!$test){
                $res = Db::name('teacher')->insertGetId($data);
                if($res){
                    Session::set('shop_id',$data['shop_id'] );
                    Session::set('uid',$res );
                    $this->success('绑定成功','index');
                }

            }else{
//                Session::set('shop_id',$test['shop_id'] );
//                Session::set('uid',$test['teacher_id'] );
                $this->error('您已绑定，请勿重复操作','index');
            }



        }



    }


}
